package com.abc.robot01.vivemodel

import android.util.Log
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.viewModelScope
import com.abc.robot01.fragment.formatDate
import com.abc.robot01.retrofit.RetrofitService
import com.abc.robot01.retrofit.bean.Condition70
import com.abc.robot01.retrofit.bean.FetchDataRequest456
import com.abc.robot01.retrofit.bean.FetchDataRequest9
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch


class SystemMessageFragmentViewModel : BaseViewModel() {
    val TAG = "SystemMessageFragmentViewModel"
    val customerName = mutableStateOf("")
    val totalAmount = mutableStateOf("15.08")
    val current = mutableStateOf(0)

    val loading = mutableStateOf(false)

    val count = mutableStateOf("0")
    val count0 = mutableStateOf("0")
    val customerPhone = mutableStateOf("")
    val show = mutableStateOf(false)
    val isLeft = mutableStateOf(false)

    val selectedDateMillis = mutableStateOf(0L)
    val startDate = mutableStateOf(0L)
    val endDate = mutableStateOf(0L)

    val selectedDateMillis0 = mutableStateOf(0L)
    val startDate0 = mutableStateOf(0L)
    val endDate0 = mutableStateOf(0L)

    fun reset0() {
        selectedDateMillis0.value = 0L
        startDate0.value = 0L
        endDate0.value = 0L
    }

    fun reset1() {
        selectedDateMillis.value = 0L
        startDate.value = 0L
        endDate.value = 0L
    }

    //客户列表
    private val _customerList = MutableStateFlow<List<RetrofitService.RenderItem>>(emptyList())
    val customerList: StateFlow<List<RetrofitService.RenderItem>> = _customerList

    //通知
    private val _customerList0 = MutableStateFlow<List<RetrofitService.Item2345678>>(emptyList())
    val customerList0: StateFlow<List<RetrofitService.Item2345678>> = _customerList0


    fun load1() {
        viewModelScope.launch {
            try {
                loading.value = true
                val res = RetrofitService.instance.getListRenderTask(
                    accessToken = Robot.accessToken,
                    companyId = Robot.companyId,
                    request = FetchDataRequest9(
                        condition = Condition70(
                        ).apply {
                            if (startDate.value != 0L) {
                                this.createTimeStart = formatDate(startDate.value) + " 00:00:00"
                            }
                            if (endDate.value != 0L) {
                                this.createTimeEnd = formatDate(endDate.value) + " 23:59:59"
                            }
                        }
                    )
                )
//            Log.e(TAG, "系统渲染记录:${res.data.list!!.size}")
//            Log.e(TAG, "系统渲染记录:$res")

                res.data.list!!.forEach {
                    Log.e(TAG, "系统渲染记录:$it")
                    Log.e(TAG, "系统渲染记录 状态:${it.status}")
                }
                if (res.code == 200) {
                    _customerList.value = res.data.list!!
                    count.value = res.data.list.size.toString()
                }
                loading.value = false
            } catch (e: Exception) {
                loading.value = false;
                e.printStackTrace();
                showToast(e.message!!)
            }
        }
    }

    fun load0() {
        viewModelScope.launch {
            loading.value = true
            try {
                val res = RetrofitService.instance.getNoticeList(
                    accessToken = Robot.accessToken,
                    companyId = Robot.companyId,
                    userId = Robot.userId,
                    request = FetchDataRequest456().apply {
                        if (startDate0.value != 0L) {
                            this.createTimeBegin = formatDate(startDate0.value) + " 00:00:00"
                        }
                        if (endDate0.value != 0L) {
                            this.createTimeEnd = formatDate(endDate0.value) + " 23:59:59"
                        }
                    }
                )
                res.data.list.forEach {
                    if (it.extData != null) {
                        Log.e(TAG, "通知列表: " + it)
                        it.extData.let {
                            if (it.housePlansId == null || it.housePlansDataId == null) {
                                throw RuntimeException("it.housePlansId==null||it.housePlansDataId==null")
                            }
                        }
                    }
                }
                _customerList0.value = res.data.list
                count0.value = res.data.list.size.toString()
                loading.value = false
            } catch (e: Exception) {
                e.printStackTrace()
                loading.value = false
                showToast(e.message!!)
            }
        }
    }


}
